$(function(){
	$('#creditWinId').window('close');
	$('#dgDepartment').datagrid().datagrid('getPager');
	$('#dgDepartment').datagrid({    
	    url:'../../department/queryDepartment',
	    rownumbers:true,
	    singleSelect:true,
	    pagination:true,
	    method:'post',
	    queryParams: {
	    	type:'3'
		},
	    columns:[[    
	       /* {field:'departmentCode',title:'部门编码',width:'28%',align:'center'},*/    
	        {field:'departmentName',title:'部门名称',width:'96%',align:'center'}    
	          
	    ]]    
	});
})

function creditWin(){
	var dg = $('#dg').datagrid('getSelected');
	if(dg==null){
		$.messager.alert("提示","请先选中一条承诺信息","error");
		return;
	}
	$('#dgDepartment').datagrid('unselectAll');
	$('#creditWinId').dialog({
		title: '分配部门信息',
		width: 350,
		height: 430,
		closed: false,
		cache: false,
		modal: true,
		buttons:[{
			text:'分配',
			iconCls:'icon-edit',
			handler:function(){
				var department = $('#dgDepartment').datagrid('getSelected');
				if(department==null){
					$.messager.alert("提示","请先选中要分配的部门名称","error");
					return;
				}
				var sysId = dg.sysId;
				var id = department.id;
				var departmentName = department.departmentName;
				$.ajax({  
					url:'../creditCommitment/updateCreditCommitment',
					type:'post',
					data:{
					"sysId":sysId,
					"departmentCode":id,
					"departmentName":departmentName
					},
					dataType:'json',
					error:function(){
						$.messager.alert("提示","操作异常","error");
					}, 
					success:function(data){ 
						var row = data.row;
						if(row>=0){
							$.messager.confirm('确认','分配部门成功',function(r){ 
							$('#dg').datagrid("reload");
							$('#creditWinId').dialog('close');
							return; 
						});
						}else{
							$.messager.alert("提示","分配部门失败","error");
						}
					}
				})
			}
				
		},{
			text:'退出',
			iconCls:'icon-no',
			handler:function(){
				$('#creditWinId').dialog('close');
			}
		}]
	});
	
	
}

//添加或者修改
function addOrUpdateCredit(type){
		var sysId='';
		if(type=='2'){
			var row = $('#dg').datagrid('getSelected');
			if(row==null){
				$.messager.alert("提示","请先选中一条记录","error");
				return;
			}
			sysId = row.sysId;
		}
		var url ="../public/add_update_creadit.jsp?type="+type+"&sysId="+sysId; 
		var backValue =window.open(url,"",
				'edge:Raised;center:Yes;help:Yes;resizable:no;'
				+'status:No;dialogWidth:1200px;DialogHeight=650px');
		if(backValue=="success"){
	    	$('#dg').datagrid("reload");
		}
}

$(function() {
	/* 分页 */
	var pager = $('#dg').datagrid().datagrid('getPager');
})
// 重新载入当前页面数据
function refresh() {
	$('#dg').datagrid('reload');
}
//启用/删除formatter
function isdelformatter(value, row, index) {
	if (value == "1") {
		return '<span><a href="#" style="text-decoration:none;color:green"'
				+ 'onclick=qidel("' + row.sysId + '",' + value
				+ '); >已 启 用</a></span>';
	} else if (value == "2") {
		return '<span><a href="#" style="text-decoration:none;color:red" '
				+ 'onclick=qidel("' + row.sysId + '",' + value
				+ '); >已 禁 用</a></span>';
	} else {
		return value;
	}
}
//启用/删除
function qidel(sysId, value) {
	var isDel = "";
	var delName = "";
	if (value == '1') {
		isDel = '2';
		delName = "禁用";
	}
	if (value == '2') {
		isDel = '1';
		delName = "启用";
	}

	$.messager.confirm('确认', '您确认想要 [ ' + delName + ' ] 数据吗？', function(r) {
		if (r) {
			$.ajax({
				url : '../creditCommitment/updateCreditCommitment',
				type : 'post',
				data : {
					"sysId" : sysId,
					"isDel" : isDel
				},
				dataType : 'json',
				error : function() {
					$.messager.alert("提示", delName + "异常", "error");
				},
				success : function(map) {
					var row = map.row;
					if (row >= 0) {
						$.messager.alert("提示", delName + "成功", "info");
						$('#dg').datagrid("reload");
						return;
					} else {
						$.messager.alert("提示", delName + "失败", "error");
						return;
					}
				}
			});
		}
	});
}
/* 点击搜索时触发 */
function init() {

	//var commitmentCode = $("#commitmentCodeMsgId").val().replace(/\s/g, "");//承诺编码
	var commitmentName = $("#commitmentNameMsgId").val().replace(/\s/g, "");//承诺名称 
	//var departmentCode = $("#departmentCodeMsgId").val().replace(/\s/g, "");//部门编码
	var departmentName = $("#departmentNameMsgId").val().replace(/\s/g, "");//部门名称
	var isDel = $("#isDelMsgId").combobox('getValue');//1显示，2禁用

	$('#dg').datagrid({
		url : '../creditCommitment/queryCreditCommitment?dt=' + new Date().getTime(),
		queryParams : {
			//commitmentCode : commitmentCode,
			commitmentName : commitmentName,
			//departmentCode : departmentCode,
			departmentName : departmentName,
			isDel : isDel
		}

	});
}
/* 点击重置时触发 */
function chongzhi() {
	$('#commitmentCodeMsgId').textbox('clear');
	$('#commitmentNameMsgId').textbox('clear');
	$('#departmentCodeMsgId').textbox('clear');
	$('#departmentNameMsgId').textbox('clear');
	$('#isDelMsgId').combobox('setValue', '');

}

//创建时间formatter
function createTimeformatter(value, row, index) {
	if (value == null || value == "") {
		return "";
	}
	var now = new Date(value);
	var time = now.format("yyyy-MM-dd hh:mm:ss");
	return time;
}
Date.prototype.format = function(format) {
	var o = {
		"M+" : this.getMonth() + 1, //month
		"d+" : this.getDate(), //day
		"h+" : this.getHours(), //hour
		"m+" : this.getMinutes(), //minute
		"s+" : this.getSeconds(), //second
		"q+" : Math.floor((this.getMonth() + 3) / 3), //quarter
		"S" : this.getMilliseconds()
	//millisecond
	}

	if (/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	}

	for ( var k in o) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
					: ("00" + o[k]).substr(("" + o[k]).length));
		}
	}
	return format;
}